//
//  TKCompleteRegisterViewController.m
//  Ticket
//
//  Created by 小禹宝宝 on 2017/10/17.
//  Copyright © 2017年 cy55. All rights reserved.
//

#import "TKCompleteRegisterViewController.h"

@interface TKCompleteRegisterViewController ()
<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet UITextField *verificationTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (assign, nonatomic) BOOL needStop;

@end

@implementation TKCompleteRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"注册";
    self.registerBtn.backgroundColor = [BLColorConfig mainColor];
    self.registerBtn.layer.cornerRadius = 15;
    self.registerBtn.layer.masksToBounds = true;
    self.getCodeBtn.backgroundColor = [BLColorConfig backgroundColor];
    //    self.getCodeBtn.layer.cornerRadius = 15;
    //    self.getCodeBtn.layer.masksToBounds = true;
    [self ks_setupLeftBackBarButtonItemWithColor:nil];
    self.verificationTextField.tintColor = [BLColorConfig mainColor];
    self.verificationTextField.textColor = [BLColorConfig textColor];
    self.verificationTextField.delegate = self;
    self.passwordTextField.tintColor = [BLColorConfig mainColor];
    self.passwordTextField.textColor = [BLColorConfig textColor];
    self.passwordTextField.delegate = self;

    if (self.phoneNumberString.length > 0) {
        self.messageLabel.text = [NSString stringWithFormat:@"验证码短信已发送到 %@",self.phoneNumberString];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (IBAction)getCodeAction:(id)sender {
    
    [self reciveSecurityCode];
    [self SecurityCodecountDown];

}

- (IBAction)registerAction:(id)sender {
    if ([self.passwordTextField.text isEqualToString:@""]) {
        [self ks_showToast:@"密码不能为空"];
        return;
    }
    if ([self.verificationTextField.text isEqualToString:@""]) {
        [self ks_showToast:@"验证码不能为空"];
        return;
    }
    
    NSDictionary *params = @{@"mobile":self.phoneNumberString,@"check_code":self.verificationTextField.text};
    @weakify(self)
    /*
    [BLApi POST:URL_Register_verifyCode parameters:params decodeClass:[BLBaseResponse class] completion:^(id responseObject, id responseDataOriginal, NSError *error) {
        @strongify(self)
        if (!error) {
            BLBaseResponse *response = (BLBaseResponse *)responseObject;
            if ([response isSuccess]) {

                
            } else {

                [self ks_showToast:response.msg];
            }
        }
    }];
     */
}

- (void)reciveSecurityCode {
    NSDictionary *params = @{@"mobile":self.phoneNumberString};
    @weakify(self)
    [BLApi POST:URL_Register_sendCode parameters:params decodeClass:nil completion:^(id responseObject, id responseDataOriginal, NSError *error) {
        @strongify(self)
        NSLog(@"%@",responseDataOriginal);
        NSLog(@"%@",responseObject);

        if (error) {
            [self ks_showToast:error.domain];
            
        }
    }];
    
}

-(void)SecurityCodecountDown {
    __block int timeout= 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0||_needStop){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
//                [_getCodeBtn setBackgroundColor:[UIColor jc_colorWithHexString:@"2962ff"]];
//                [_getCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                
                _getCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                NSLog(@"____%@",strTime);
                [_getCodeBtn setTitle:[NSString stringWithFormat:@"倒计时(%@)",strTime] forState:UIControlStateNormal];
//                [_getCodeBtn setBackgroundColor:ButtonColorWithGray];
//                [_getCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

                _getCodeBtn.userInteractionEnabled = NO;
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField  == self.verificationTextField) {
        [self.passwordTextField becomeFirstResponder];
    }
    if (textField ==self.passwordTextField) {
        [self.passwordTextField resignFirstResponder];
    }
    return YES;
}
@end
